package com.witdata.common.service.course.impl;

import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.web.multipart.MultipartFile;

import com.mybatisflex.core.query.QueryWrapper;
import com.witdata.common.domain.Status;
import com.witdata.common.domain.dto.CourseDto;
import com.witdata.common.domain.vo.CourseVo;
import com.witdata.common.domain.vo.UserVo;
import com.witdata.common.exception.GlobalException;
import com.witdata.common.mapper.UserMapper;
import com.witdata.common.service.FileService;
import com.witdata.common.service.course.CourseService;
import com.witdata.common.service.course.CourseUsersService;
import com.witdata.common.util.IdUtils;
import com.witdata.mysql.base.service.CoursesBaseService;
import com.witdata.mysql.base.service.UserBaseService;
import com.witdata.mysql.base.service.CourseMembersBaseService;
import com.witdata.mysql.base.service.CourseSchedulesBaseService;
import com.witdata.mysql.domain.entity.TCourseMembers;
import com.witdata.mysql.domain.entity.TUser;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;


@Service
@Slf4j
@RequiredArgsConstructor
public class CourseUsersServiceImpl implements CourseUsersService {

    private final CourseMembersBaseService courseMembersBaseService;
    private final UserBaseService userBaseService;
    private final CoursesBaseService coursesBaseService;


    @Override
    public Boolean addUser(Long courseId, Long userId, Integer type) {
        // 判断用户是否存在
        if (ObjectUtils.isEmpty(userId)) throw new GlobalException(Status.PARAMERROR);
        Boolean exists = userBaseService.exists(new QueryWrapper().eq("id", userId));
        if (!exists) throw new GlobalException(Status.USERNOTFOUND);
        // 添加用户
        return courseMembersBaseService.save(new TCourseMembers(IdUtils.createId(), courseId, userId, type, Timestamp.valueOf(LocalDateTime.now()), true));
    }

    @Override
    public Boolean removeUser(Long courseId, Long userId) {
        // 判断用户是否存在
        if (ObjectUtils.isEmpty(userId)) throw new GlobalException(Status.PARAMERROR);
        Boolean exists = courseMembersBaseService.exists(new QueryWrapper().eq("course_id", courseId).eq("user_id", userId));
        if (!exists) throw new GlobalException(Status.USERNOTFOUND);
        // 删除用户
        return courseMembersBaseService.remove(new QueryWrapper().eq("course_id", courseId).eq("user_id", userId));
    }

    @Override
    public List<CourseVo> getUserCourseList(Long userId) {
        // 判断用户是否存在
        if (ObjectUtils.isEmpty(userId)) throw new GlobalException(Status.PARAMERROR);
        Boolean exists = courseMembersBaseService.exists(new QueryWrapper().eq("user_id", userId));
        if (!exists) throw new GlobalException(Status.USERNOTFOUND);
        // 获取用户课程列表
        List<TCourseMembers> list = courseMembersBaseService.list(new QueryWrapper().eq("user_id", userId));
        List<Long> courseIds = list.stream().map(TCourseMembers::getCourseId).collect(Collectors.toList());
        return coursesBaseService.listByIds(courseIds).stream().map(CourseVo::new).collect(Collectors.toList());
    }
    @Override
    public List<UserVo> getCourseUserList(Long courseId, Integer type) {
        // 判断课程是否存在
        if (ObjectUtils.isEmpty(courseId)) throw new GlobalException(Status.PARAMERROR);
        Boolean exists = coursesBaseService.exists(new QueryWrapper().eq("id", courseId));
        if (!exists) throw new GlobalException(Status.COURSENOTFOUND);
        // 获取课程用户列表
        List<TCourseMembers> list = courseMembersBaseService.list(new QueryWrapper().eq("course_id", courseId).eq("role", type));
        List<Long> userIds = list.stream().map(TCourseMembers::getUserId).collect(Collectors.toList());
        List<TUser> tUserList = userBaseService.listByIds(userIds);
        return UserMapper.INSTANCE.toUserVoList(tUserList);
    }
}